package com.jeesite.modules.storage.api;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.storage.util.WebUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * 基础Controller
 */
@CrossOrigin
@RestController(value = "api")
public class APIController
{
    /*
    * 报错图片
     * @param file 文件
     * @param filePath 文件地址
     * @return 地址
    * */
    protected String save(MultipartFile file,String filePath){
        if(StringUtils.isBlank(filePath)){
            filePath = "/image/" + UUID.randomUUID() + WebUtil.getExtension(file.getOriginalFilename());
        }
        WebUtil.checkFile(WebUtil.getRootPath(filePath));
        File tempFile = new File(WebUtil.getRootPath(filePath));
        try {
            file.transferTo(tempFile);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return filePath;
    }

    /*
    * 获取JSON参数
    * */
    protected String getJsonParam(String param){
        if(StringUtils.isBlank(param)){
            return "";
        }
        param = param.replaceAll("【【","[").replaceAll("】】","]").replaceAll("￥【","{").replaceAll("】￥","}").replaceAll("＂","\"");
        return param;
    }
}
